<?php
	/**
	 * 管理分类控制器
	 * @author Lwbang 
	 * @author laiwenbang <375500819@qq.com>
	 */
	 
	 namespace Admin\Controller;
	 
	class CategoryController extends AuthController {
		
		private $model;
		
		protected function _initialize(){
			parent::_initialize();
			$this->model = D('Category');
		}
		
		/************************************
		 * 
		 * [显示所有分类的首页]
		 *	@return void 
		 ************************************/
		public function index($flag=false){
			//返回所有分类树
			$tree = $this->model->getTree();
			//查询每个分类有多少篇文章
			foreach($tree as $k => $t){
				$tree[$k]['count'] = M('Article')->where(array('post_category_id'=>$t['category_id']))->count();
			}
			//如果为false那么默认输出页面否则 就输出成数组
			if(!$flag){
				$this->assign('trees',$tree);
				$this->display();	
			}
			return $tree;
			
		}
		
		
		/************************************
		 * 
		 * [添加分类]
		 * 
		 * @return {String}
		 * 
		 ************************************/
		public function addCategory($data){
			if($this->model->create($data)){
				$this->model->add($data);
				$tree = $this->getTreeHtml();
				$this->ajaxReturn(array('status'=>1,'msg'=>'分类添加成功','str'=>$tree['str'],'select'=>$tree['select']));
			}
			
			$this->ajaxReturn(array('status'=>0,'msg'=>$this->model->getError()));
			
		}
		
		/**
		 * 返回纯HTML分类表格
		 * @return Array
		 */
		private function getTreeHtml(){
				//重新组表格
				$str = '';
				//重新组下拉菜单 
				$select = '';
				//返回新的树结构
				$tree = $this->index(true);
				
				foreach($tree as $data){
					$str .= '<tr data-cid="'.$data['category_id'].'" data-pid="'.$data['category_pid'].'" data-data="'.$data['category_id'].'|'.$data['category_name'].'|'.$data['category_pid'].'">';
					$str .=	'<td>'.$data['category_id'].'</td>';
					$str .= '<td><a href="">'.$data['_name'].'</a></td>';
					$str .= '<td>'.$data['count'].'</td>';
					$str .= '<td class="editBtnBox">';
					$str .= '<a href="javascript:;" class="btn btn-xs btn-success editBtn"><span class="glyphicon glyphicon-pencil"></span>编辑</a> ';
					$str .= '<a href="javascript:;" class="btn btn-xs btn-danger delBtn"><span class="glyphicon glyphicon-trash">删除</a> ';
					$str .= '<a href="javascript:;" class="btn btn-xs btn-info showBtn"><span class="glyphicon glyphicon-eye-open"></span>查看</a>';	
					$select .= '<option value="'.$data['category_id'].'">'.$data['_name'].'</option>';
				}
			return array('str'=>$str,'select'=>$select);
		}
		
		
		
		/**
		 * 修改分类 首先检测当前分类是否已经存在如果已经存在
		 * 那么返回一个ajax
		 * 
		 */
		public function editCategory(){
			//当前分类ID
			$category_id 	= I('post.category_id',0,'intval');
			//当前分类名
			$category_name 	= I('post.category_name','','strval');
			//当前分类pid
			$category_pid 	= I('post.category_pid',0,'intval');
			//查询当前的分类名是否已经存在 
			$cname = $this->model->where("category_name='{$category_name}' AND category_id != {$category_id}")->find();
			if($cname){
				$this->ajaxReturn(array('status'=>0,'msg'=>'分类已经存在请更换'));
			}
			$this->model->save(I('post.'));
			//重新返回分类表格和下拉表单
			$tree = $this->getTreeHtml();
			$this->ajaxReturn(array('status'=>1,'msg'=>'分类修改成功','str'=>$tree['str'],'select'=>$tree['select']));
			
		}
		
		
		/************************************
		 * 
		 * 删除一个分类，如果该分类存在子类和文章那么将无法删除
		 * 
		 * @return {String}
		 * 
		 ************************************/
		public function delCategory(){
			if(IS_AJAX){
				$cid = I('post.cid');	
				$flag = $this->model->isDelCategory($cid);
				//存在子分类
				if($flag===2){
					$this->ajaxReturn(array('status'=>2,'msg'=>'删除分类失败,该分类存在子分类请先删除子分类'));
				}
				//存在文章
				if($flag===3){
					$this->ajaxReturn(array('status'=>2,'msg'=>'删除分类失败,该分类存在文章。'));
				}
				//表示删除成功
				$tree = $this->getTreeHtml();
				$this->ajaxReturn(array('status'=>1,'msg'=>'删除分类成功','str'=>$tree['str'],'select'=>$tree['select']));
			}
			$this->error('非法请求');
		}
		
		
		/************************************
		 * 
		 * [检测当前要添加的分类是否已经存在] 
		 * @return {String} {变量}
		 * 
		 ************************************/
		public function checkCategory(){
			//获得要添加的分类名
			$categoryName = I('post.category_name','','strval');
			//检测分类名是否已经存在如果存在返回一个ajax
			$isExists = $this->model->where(array('category_name'=>$categoryName))->find();
			if($isExists){
				$this->ajaxReturn(array('ststus'=>0,'msg'=>'添加的分类名已经存在'));
			}
			//那么就写入
			$this->addCategory(I('post.'));
			
		}
		
		
	}

